package com.zp.elasticsearch.controller;

import com.zp.elasticsearch.config.ElasticSearchClient;
import com.zp.elasticsearch.vo.ExampleEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
/**
 * <p>
 * 规则动作定义信息 前端控制器
 * </p>
 *
 * @author 张鹏
 * @since 2017-12-15
 */
@RestController
@RequestMapping("/els")
@Api(tags = "TestController", description = "els增删改查", hidden = true)
public class TestController{
	@Value("${es.ip}")
	private  String esIp;
    @Autowired
    private ElasticSearchClient elasticSearchClient;

    @GetMapping("/save")
    @ApiOperation(value = "新增/更新")
    public String  getByIds(String id){
		ExampleEntity entity = new ExampleEntity() ;
		entity.setCreated(new Date());
		entity.setId(id);
		entity.setMessage("只要键值是按照标准格式定义的，就无需担忧在 @ConditionalOnProperty: 中键值的结构，被支持的宽松的变量将会自动透明的工作。如果你正在使用 prefix 属性，你可以简单地通过使用 name 或者value 属性 放入全键值。");
		elasticSearchClient.save(entity);
		return "SUCCESS";
    }


	@GetMapping("/del")
	@ApiOperation(value = "删除")
	public String delIds(String id){
		elasticSearchClient.delById(id);
		return "SUCCESS";
	}

	@GetMapping("/search")
	@ApiOperation(value = "新增/更新")
	public List<ExampleEntity> search(String mess){
		BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
		queryBuilder.must(QueryBuilders.multiMatchQuery(
				mess,
				"message",
				"id"
		));
		List<ExampleEntity> results  = elasticSearchClient.search(queryBuilder);
		return results;
	}

}

